/*
 * ESBDataProvider.java
 *
 * Created on Aug 27, 2007, 1:38:31 PM
 *
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.oslo.data.ESB;

import java.rmi.server.UID;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageListener;
import javax.jms.MessageProducer;
import javax.jms.ObjectMessage;
import javax.jms.Queue;
import javax.jms.Session;
import org.apache.activemq.ActiveMQConnection;
import org.oslo.data.DataException;
import org.oslo.data.DataProvider;
import org.oslo.domain.HotelInfo;
import org.oslo.domain.ImageInfo;
import org.oslo.domain.LocationInfo;
import org.oslo.domain.SearchInfo;
import org.oslo.domain.SearchResult;
import org.oslo.ui.OSLOApp;
import org.oslo.ui.OSLOView;

/**
 *
 * @author nl24167
 */
public class ESBDataProvider implements DataProvider, MessageListener {

    private ActiveMQConnection connection = null;
    private Session session;

    private Queue destination;
    private Queue responseDestination;
    private Queue searchDestination;
    private Queue searchResponseDestination;

    private MessageConsumer locationConsumer;
    private MessageProducer locationProducer;
    private MessageProducer searchProducer;
    private MessageConsumer searchConsumer;


    public ESBDataProvider() {
        System.out.println(OSLOApp.getApplication().getMainFrame().getClass());
    }

    @SuppressWarnings(value = "unchecked")
    public List<ImageInfo> getSessionImages(String conferenceName, String conferenceLocation) throws DataException {
        List<ImageInfo> resultImages = new ArrayList<ImageInfo>();
        try {
            ObjectMessage searchMessage = session.createObjectMessage();
            SearchInfo search = new SearchInfo();
            search.setConferenceName(conferenceName);
            search.setConferenceLocation(conferenceLocation);
            search.setImageCount(5);
            searchMessage.setObject(search);
            searchProducer.send(searchMessage);
            
        } catch (Exception exception) {
            throw new DataException("Error retrieving message from response queue", exception);
        }

        return resultImages;
    }

    public LocationInfo getConferenceLocation(String conferenceName, String conferenceLocation) throws DataException {
        try {
            ObjectMessage searchMessage = session.createObjectMessage();
            SearchInfo search = new SearchInfo();
            search.setConferenceName(conferenceName);
            conferenceLocation = conferenceLocation.replace(' ', '+');
            search.setConferenceLocation(conferenceLocation);
            search.setSearchIdentifier(new UID().toString());
            searchMessage.setObject(search);

            System.out.println("123");
            
            locationProducer.send(searchMessage);
            System.out.println("321");
        } catch (Exception exception) {
            throw new DataException("Error retrieving message from response queue", exception);
        }
        
        return null;
    }

    public List<HotelInfo> getHotels(String conferenceName, String conferenceLocation) throws DataException {
        try {
            ObjectMessage searchMessage = session.createObjectMessage();
            SearchInfo search = new SearchInfo();
            search.setConferenceName(conferenceName);
            search.setConferenceLocation(conferenceLocation);
            search.setSearchIdentifier(new UID().toString());
            searchMessage.setObject(search);
            search.setImageCount(0);

            searchProducer.send(searchMessage);
        } catch (Exception exception) {
            throw new DataException("Error retrieving message from response queue", exception);
        }
        
        return null;
    }

    public void init() {
        try {
            connection = org.apache.activemq.ActiveMQConnection.makeConnection("tcp://localhost:61616");
            
            session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);

            destination = session.createQueue("conference.in");
            searchDestination = session.createQueue("search.in");
            searchResponseDestination = session.createQueue("search.out");
            responseDestination = session.createQueue("conference.out");

            locationProducer = session.createProducer(destination);
            locationConsumer = session.createConsumer(responseDestination);
            searchProducer = session.createProducer(searchDestination);
            searchConsumer = session.createConsumer(searchResponseDestination);
            
            searchConsumer.setMessageListener(this);
            locationConsumer.setMessageListener(this);
            
            connection.start();
            
        } catch (Exception ex) {
            throw new RuntimeException("Error initializing data connection, cannot continue", ex);
        }
    }

    @SuppressWarnings("unchecked")
private void handleResponse(ObjectMessage responseMessage) throws Exception {                
        
        Object response = responseMessage.getObject();
        System.out.println("sdjfklsd");
        
        if (response instanceof LocationInfo) {
             OSLOView.getInstance().updateLocation((LocationInfo)response);
        }
        
        if (response instanceof SearchResult) {
            SearchResult searchResult = (SearchResult)response;
            if (searchResult.getLocationInfo() != null) {
                OSLOView.getInstance().updateLocation(searchResult.getLocationInfo());
            } 
            
            if (searchResult.getSessionsTO() != null) {
                System.out.println("searchResult: " + searchResult.getSessionsTO().getSessionList().size() ); 
                OSLOView.getInstance().setSessions(searchResult.getSessionsTO().getSessionList());
            }
            
            
        } else if (response instanceof List) {
        List objectList = (List) responseMessage.getObject();
        Iterator itObject = objectList.iterator();
        int counter = 1;
        while (itObject.hasNext()) {
            Object responseObject = itObject.next();
            if (responseObject instanceof HotelInfo) {
                HotelInfo hotelInfo = (HotelInfo) responseObject;
                OSLOView.getInstance().addHotel(hotelInfo);
            } else {
                ImageInfo imageInfo = (ImageInfo) responseObject;
                OSLOView.getInstance().addImage(imageInfo);
                counter++;
            }
        }            
        }
    }

    public void onMessage(Message message) {
        try {
            handleResponse((javax.jms.ObjectMessage) message);
        } catch (Exception ex) {
            ex.printStackTrace();;
        }
    }
}
